<script>
import { updateStudent } from '@/api/admin/student.js'
import studentRules from '@/rules/studentRules.js'
import { selectClassOptions } from '@/api/authApi.js'
export default {
  props: ['modelValue'],
  emits: ['update:modelValue', 'resetData'],
  computed: {
    flag: {
      get() {
        return this.modelValue
      },
      set(nv) {
        this.$emit('update:modelValue', nv)
      },
    },
  },
  data() {
    return {
      studentRules,
      classes: [],
      params: {
        class_id: '',
      },
      studentData: {
        student_number: '',
        student_name: '',
        student_email: '',
        student_birthday: '',
        student_sex: '',
        student_class_id: '',
      },
    }
  },

  methods: {
    async initClassOptions() {
      this.classes = await selectClassOptions(this.params)
    },
    remoteMethod(key) {
      this.params.class_id = key
      this.initClassOptions()
    },
    async EditNewStudent() {
      await this.$refs.form.validate()
      await updateStudent(this.studentData)
      this.flag = false
      this.$emit('resetData')
    },
    resetFormData() {
      this.studentData = {}
      this.$refs.form.resetFields()
    },
    setDefaultForm(obj) {
      const studentSex = obj.student_sex?.toString() || '1'

      obj.student_id = obj.student_id
      obj.student_number = obj.student_number
      obj.student_name = obj.student_name
      obj.student_email = obj.student_email
      obj.student_birthday = obj.student_birthday
      obj.student_sex = studentSex
      obj.student_class_id = obj.class_id
      this.studentData = obj
    },
  },
  created() {
    this.initClassOptions()
  },
}
</script>

<template>
  <el-dialog v-model="flag" title="学生信息更新" width="600px" @closed="resetFormData">
    <el-form
      label-width="80px"
      :inline="false"
      ref="form"
      :model="studentData"
      :rules="studentRules"
    >
      <el-form-item label="学生姓名" prop="student_name">
        <el-input type="text" v-model="studentData.student_name"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="student_number">
        <el-input type="text" v-model="studentData.student_number"></el-input>
      </el-form-item>
      <el-form-item label="出生日期" prop="student_birthday">
        <el-date-picker
          class="datetime"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          v-model="studentData.student_birthday"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="class_name">
        <el-input type="text" v-model="studentData.student_email"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="student_sex">
        <el-radio-group v-model="studentData.student_sex">
          <el-radio value="1" size="large">男</el-radio>
          <el-radio value="0" size="large">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="班级" prop="student_class_id">
        <el-select
          clearable
          v-model="studentData.student_class_id"
          filterable
          remote
          :remote-method="remoteMethod"
        >
          <el-option
            v-for="c in classes"
            :key="c.class_id"
            :value="c.class_id"
            :label="c.class_name"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="$emit('update:modelValue', nv)">取消</el-button>
      <el-button type="primary" @click="EditNewStudent()">更新</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>
:deep(.datetime) {
  width: 100%;
}
</style>
